import 'dart:ui';

import 'package:base_plugin/base_plugin.dart';
import 'package:flutter/material.dart';

class CropBorder extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: CropBorderPainter(),
    );
  }
}

class CropBorderPainter extends CustomPainter {
  get frameWidth => getDp(40);

  get frameStrokeWidth => getDp(8);

  get rectStrokeWidth => getDp(3);

  @override
  void paint(Canvas canvas, Size size) {
    double width = size.width;
    double height = size.height;
    Paint mPaint = Paint()
      ..color = Colors.white
      ..strokeWidth = rectStrokeWidth
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round;
    canvas.drawRect(
        Offset(frameStrokeWidth / 2, frameStrokeWidth / 2) &
            Size(width - frameStrokeWidth, height - frameStrokeWidth),
        mPaint);

    mPaint.strokeWidth = frameStrokeWidth;
    // top left
    canvas.drawPoints(
        PointMode.polygon,
        [
          Offset(frameStrokeWidth / 2, frameWidth + frameStrokeWidth / 2),
          Offset(frameStrokeWidth / 2, frameStrokeWidth / 2),
          Offset(frameStrokeWidth / 2 + frameWidth, frameStrokeWidth / 2),
        ],
        mPaint);

    // top right
    canvas.drawPoints(
        PointMode.polygon,
        [
          Offset(
              width - frameWidth - frameStrokeWidth / 2, frameStrokeWidth / 2),
          Offset(width - frameStrokeWidth / 2, frameStrokeWidth / 2),
          Offset(
              width - frameStrokeWidth / 2, frameWidth + frameStrokeWidth / 2),
        ],
        mPaint);

    // bottom left
    canvas.drawPoints(
        PointMode.polygon,
        [
          Offset(
              frameStrokeWidth / 2, height - frameWidth - frameStrokeWidth / 2),
          Offset(frameStrokeWidth / 2, height - frameStrokeWidth / 2),
          Offset(
              frameWidth + frameStrokeWidth / 2, height - frameStrokeWidth / 2),
        ],
        mPaint);

    // bottom right
    canvas.drawPoints(
        PointMode.polygon,
        [
          Offset(width - frameStrokeWidth / 2,
              height - frameWidth - frameStrokeWidth / 2),
          Offset(width - frameStrokeWidth / 2, height - frameStrokeWidth / 2),
          Offset(width - frameWidth - frameStrokeWidth / 2,
              height - frameStrokeWidth / 2),
        ],
        mPaint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
